import React, { useReducer, useState } from "react";
import queryApi from "../../api/query";

const FormQueryContext = React.createContext();

const dataReducer = (state, action) => {
  switch (action.type) {
    case "add_data":
      return state;
    case "edit_data":
      // console.log("--value--", [
      //   action.payload.value0,
      //   action.payload.value1,
      //   action.payload.value2,
      // ]);
      // return action.payload.value;
      return [
        action.payload.value0,
        action.payload.value1,
        action.payload.value2,
      ];
    case "sort_data":
      let page = action.payload.page;
      let way = action.payload.way;
      let arr = state[1][page];
      let len = arr.length;
      // console.log("arr=================", way);
      for (let i = 0; i < len; i++) {
        for (let j = 0; j < len - 1 - i; j++) {
          if (way == 1) {
            if (parseFloat(arr[j].price) > parseFloat(arr[j + 1].price)) {
              let temp = arr[j + 1];
              arr[j + 1] = arr[j];
              arr[j] = temp;
            }
          } else if (way == 0) {
            if (parseFloat(arr[j].price) < parseFloat(arr[j + 1].price)) {
              let temp = arr[j + 1];
              arr[j + 1] = arr[j];
              arr[j] = temp;
            }
          }
        }
      }
      // console.log("排序后-----", arr);
      state[1][page] = arr;
      return state;
    default:
      return state;
  }
};

let initData = [
  // 最低价
  [
    // {
    //   amount: 11,
    //   basePlate: {
    //     category: "铜",
    //     density: 1.3,
    //     pro_price: [222, "元/㎡"],
    //     res_price: 6443.782400000002,
    //     type: "tong1",
    //     unit_price: [202, "元/㎡"],
    //   },
    //   company: {
    //     address: "陕西省西安市",
    //     admin_name: "曾憨憨",
    //     admin_phone: "13568989777",
    //     company_name: "曾憨憨有限公司",
    //     detailed_address: "陕西师范大学长安校区",
    //   },
    //   price: "157884.73",
    //   shroudPlate: {
    //     category: "铜",
    //     density: 1.67,
    //     pro_price: [222, "元/kg"],
    //     res_price: 151440.94400000002,
    //     type: "QF87",
    //     unit_price: [250, "元/kg"],
    //   },
    // },
    // {
    //   amount: 11,
    //   basePlate: {
    //     category: "铜",
    //     density: 1.67,
    //     pro_price: [100, "元/kg"],
    //     res_price: 177659.94400000002,
    //     type: "QF87",
    //     unit_price: [250, "元/kg"],
    //   },
    //   company: {
    //     address: "陕西省西安市",
    //     admin_name: "曾憨憨",
    //     admin_phone: "13568989777",
    //     company_name: "曾憨憨有限公司",
    //     detailed_address: "陕西师范大学长安校区",
    //   },
    //   price: "180523.62",
    //   shroudPlate: {
    //     category: "铜",
    //     density: 1.34,
    //     pro_price: [100, "元/㎡"],
    //     res_price: 2863.6800000000003,
    //     type: "tong4",
    //     unit_price: [170, "元/㎡"],
    //   },
    // },
  ],
  //同一公司
  [
    // {
    //   amount: 11,
    //   basePlate: {
    //     category: "铜",
    //     density: 1.67,
    //     pro_price: [100, "元/kg"],
    //     res_price: 177659.94400000002,
    //     type: "QF87",
    //     unit_price: [250, "元/kg"],
    //   },
    //   company: {
    //     address: "陕西省西安市",
    //     admin_name: "曾憨憨",
    //     admin_phone: "13568989777",
    //     company_name: "曾憨憨有限公司",
    //     detailed_address: "陕西师范大学长安校区",
    //   },
    //   price: "180523.62",
    //   shroudPlate: {
    //     category: "铜",
    //     density: 1.34,
    //     pro_price: [100, "元/㎡"],
    //     res_price: 2863.6800000000003,
    //     type: "tong4",
    //     unit_price: [170, "元/㎡"],
    //   },
    // },
    // {
    //   amount: 11,
    //   basePlate: {
    //     category: "铜",
    //     density: 1.67,
    //     pro_price: [100, "元/kg"],
    //     res_price: 177659.94400000002,
    //     type: "QF87",
    //     unit_price: [250, "元/kg"],
    //   },
    //   company: {
    //     address: "陕西省西安市",
    //     admin_name: "曾憨憨",
    //     admin_phone: "13568989777",
    //     company_name: "曾憨憨有限公司",
    //     detailed_address: "陕西师范大学长安校区",
    //   },
    //   price: "180523.62",
    //   shroudPlate: {
    //     category: "铜",
    //     density: 1.34,
    //     pro_price: [100, "元/㎡"],
    //     res_price: 2863.6800000000003,
    //     type: "tong4",
    //     unit_price: [170, "元/㎡"],
    //   },
    // },
  ],
  // 所有商品
  [
    [
      // {
      //   amount: 11,
      //   basePlate: {
      //     category: "铜",
      //     density: 1.67,
      //     pro_price: [100, "元/kg"],
      //     res_price: 177659.94400000002,
      //     type: "QF87",
      //     unit_price: [250, "元/kg"],
      //   },
      //   company: {
      //     address: "陕西省西安市",
      //     admin_name: "曾憨憨",
      //     admin_phone: "13568989777",
      //     company_name: "曾憨憨有限公司",
      //     detailed_address: "陕西师范大学长安校区",
      //   },
      //   price: "180523.62",
      //   shroudPlate: {
      //     category: "铜",
      //     density: 1.34,
      //     pro_price: [100, "元/㎡"],
      //     res_price: 2863.6800000000003,
      //     type: "tong4",
      //     unit_price: [170, "元/㎡"],
      //   },
      // },
    ],
    [
      // {
      //   amount: 11,
      //   basePlate: {
      //     category: "铜",
      //     density: 1.3,
      //     pro_price: [222, "元/㎡"],
      //     res_price: 6443.782400000002,
      //     type: "tong1",
      //     unit_price: [202, "元/㎡"],
      //   },
      //   company: {
      //     address: "陕西省西安市",
      //     admin_name: "曾憨憨",
      //     admin_phone: "13568989777",
      //     company_name: "曾憨憨有限公司",
      //     detailed_address: "陕西师范大学长安校区",
      //   },
      //   price: "157884.73",
      //   shroudPlate: {
      //     category: "铜",
      //     density: 1.67,
      //     pro_price: [222, "元/kg"],
      //     res_price: 151440.94400000002,
      //     type: "QF87",
      //     unit_price: [250, "元/kg"],
      //   },
      // },
    ],
  ],
];

export const FormQueryProvider = ({ children }) => {
  const [isLoading, setIsLoading] = useState(true);

  const [formQueryData, dispatch] = useReducer(dataReducer, initData);
  // console.log("initFormQueryData---", formQueryData);

  const addData = () => {
    dispatch({ type: "add_data" });
  };

  const editData = (value) => {
    // console.log("------", value);
    dispatch({
      type: "edit_data",
      payload: { value },
    });
  };

  const selectPrice = async (quoData) => {
    setIsLoading(true);
    try {
      console.log(quoData);
      // console.log("开始查");
      const formQueryData = await queryApi.post("/goods/cal", quoData);
      let resData = formQueryData.data.data;
      const companiesData = await queryApi.get("/company");
      let companies = companiesData.data.message;
      // console.log("查到公司啦---", companies);
      console.log("查完了---", resData);
      // 最低价
      let sortData = [];
      for (let k = 0; k < resData.length; k++) {
        let arr = resData[k];
        let len = arr.length;
        for (let i = 0; i < len; i++) {
          for (let j = 0; j < len - 1 - i; j++) {
            {
              if (parseFloat(arr[j].price) > parseFloat(arr[j + 1].price)) {
                let temp = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = temp;
              }
            }
          }
        }
        sortData.push(arr[0]);
      }
      // console.log("最低价---", sortData);
      // 同一公司
      let comData = [];
      for (let i = 0; i < companies.length; i++) {
        // console.log("公司---", companies[i]);
        let arr = [];
        for (let j = 0; j < resData.length; j++) {
          let goodsItem = resData[j];
          // console.log("商品", j, "---", goodsItem);
          for (let k = 0; k < goodsItem.length; k++) {
            if (
              companies[i].company_name == goodsItem[k].company.company_name
            ) {
              arr.push(goodsItem[k]);
              break;
            }
          }
        }
        if (arr.length == resData.length) {
          comData.push(arr);
        }
      }
      // console.log("同一公司---", comData);
      dispatch({
        type: "edit_data",
        payload: {
          value0: sortData,
          value1: comData,
          value2: resData,
        },
      });
      setIsLoading(false);
    } catch (error) {
      setIsLoading(false);
      console.log("err---", error);
    }
  };

  const sortPrice = (page, way) => {
    console.log("排序");
    dispatch({
      type: "sort_data",
      payload: { page: page, way: way },
    });
  };

  return (
    <FormQueryContext.Provider
      value={{
        isLoading,
        selectPrice,
        formQueryData: formQueryData,
      }}
    >
      {children}
    </FormQueryContext.Provider>
  );
};

export default FormQueryContext;
